import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/models/common_model.dart';
import 'package:flutter_demo/models/home_model.dart';
import 'package:flutter_demo/models/sales_box_model.dart';
import 'package:flutter_demo/widget/GridNavNew.dart';
import 'package:flutter_demo/widget/LoadingContainer.dart';
import 'package:flutter_demo/widget/LocalNav.dart';
import 'package:flutter_demo/widget/SubNav.dart';
import 'package:flutter_demo/widget/sales_box.dart';
import 'package:flutter_demo/widget/webview.dart';
import 'package:flutter_swiper/flutter_swiper.dart';


class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

// 固定写法
class _HomePageState extends State<HomePage> {


  double appBarAlpha = 0;
  List<CommonModel> localNavList = [];
//  GridNavModel gridNavModel;
  List<CommonModel> subNavList = [];
  SalesBoxModel salesBoxModel;
  bool _isLoading = true;
  List<CommonModel> bannerList = [];

  @override
  void initState() {
    super.initState();
    _handlerPage();
  }

  // 获取首页数据方法
  Future<Null> _handlerPage() async {
    Future<String> loadString = DefaultAssetBundle.of(context).loadString("jsons/homeJson.json");
    loadString.then((data) {
      Utf8Decoder utf8decoder = Utf8Decoder(); // fix 中文乱码
      var result = json.decode(data);
      HomeModel model = HomeModel.fromJson(jsonDecode(data));
      setState(() {
        bannerList = model.bannerList;
        localNavList = model.localNavList;
        subNavList = model.subNavList;
        salesBoxModel = model.salesBox;
        _isLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: LoadingContainer(
          child: Stack(
            children: <Widget>[
              MediaQuery.removePadding(
                  removeTop: true,
                  context: context,
                  child: NotificationListener(
                    onNotification: (scrollNotification) {
                      return null;
                    },
                    child: RefreshIndicator(
                        child: ListView(
                          children: <Widget>[
                            Container(
                                height: 262,
                                child: Stack(
                                children: <Widget>[
                                  Container(
                                    height: 210,
                                    width: MediaQuery.of(context).size.width,
                                    child: Swiper(
                                        itemCount: bannerList.length,
                                        autoplay: true,
                                        itemBuilder: (BuildContext context, int index) {
                                          return GestureDetector(
                                            onTap: () {
                                              CommonModel model = bannerList[index];
                                              Navigator.push(
                                                context,
                                                MaterialPageRoute(
                                                  builder: (context) => WebView(
                                                    url: model.url
                                                  ),
                                                ),
                                              );
                                            },
                                            child: Image.network(
                                              bannerList[index].icon,
                                              fit: BoxFit.fill,
                                            ),
                                          );
                                        },
                                        pagination: new SwiperPagination(),
                                    ),
                                  ),
                                  Positioned(
                                    top: 188,
                                    child: Container(
                                      width: MediaQuery.of(context).size.width,
                                      padding: EdgeInsets.fromLTRB(14, 0, 14, 0),
                                      child: LocalNav(
                                        localNavList: localNavList,
                                      ),
                                    ),
                                  )
                                ],
                              )
                            ),
                            Container(
                              width: MediaQuery.of(context).size.width,
                              padding: EdgeInsets.fromLTRB(14, 10, 14, 0),
                              child: Column(
                                children: <Widget>[
                                  GridNavNew(),
                                  Padding(padding: EdgeInsets.only(top: 10)),
                                  SubNav(subNavList: subNavList),
                                  Padding(padding: EdgeInsets.only(top: 10)),
                                  SalesBox(
                                      salesBoxModel: salesBoxModel
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                        onRefresh: () {
                          return null;
                        }
                    ),
                  )
              ),
              _appBar()
            ],
          ), loading: _isLoading, cover: false,),
//      appBar: Container(
//        child: AppBar(
//          backgroundColor: Colors.transparent,
//
//        ),
//      ),
    );
  }


  /// 首页的顶部的搜索栏
  Widget _appBar() {
    return Column(
      children: <Widget>[
        // 搜索栏
        Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
              //AppBar渐变遮罩背景
              colors: [Color(0x66000000), Colors.transparent],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Container(
            padding: EdgeInsets.fromLTRB(14, 20, 0, 0),
            height: 86.0,
            decoration: BoxDecoration(
                color: Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
                boxShadow: [BoxShadow(
                  color: appBarAlpha == 1.0 ? Colors.black12 : Colors.transparent,
                  offset: Offset(2, 3),
                  blurRadius: 6,
                  spreadRadius: 0.6,
                ),]
            ),
            child: TextField(
              decoration: InputDecoration(
                  border: OutlineInputBorder(borderRadius: BorderRadius.circular(15.0))
              ),
            ),
          )
        ),
        // 边框
        Container()
      ],
    );
  }
}
